
import 'package:flutter/material.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/operation/rb_enum.dart';
import 'package:reabampos/src/reabam/ui/setting/bean/bean_appoint_printer_scene.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
socket 指定打印机设置
 */
class SocketPrinterSettingStatePage extends XPageListViewState{

  SocketPrinterSettingStatePage();

  @override
  Widget getListItemWidget(listItem, int removeUsePosition) {
    BeanAppointPrinterScene item=listItem as BeanAppointPrinterScene;
    final List<BeanAppointPrinterIndex> printerIndexs=item.printerIndexs;

    List<Widget> rightWidgets=[];

    for(BeanAppointPrinterIndex itemIndex in printerIndexs){
      rightWidgets.add(
          XWidgetUtils.getLayoutContainer(
            child: XWidgetUtils.getWidgetCheckbox(
                isCurrentSelect: itemIndex.isUserSelect,
                text: itemIndex.name,
                style: XStyleUtils.getTextStyle(
                    fontSize: XTempData.defaultSmallTextFontSize,
                    color: XColorUtils.color333333
                ),
                onChanged: (bool? value){
                  setState(() {
                    itemIndex.isUserSelect=value??false;
                  });
                }
            )
          )

      );
    }

    return XWidgetUtils.getXBarForLeftRightWidget(
      margin: XWidgetUtils.getEdgeInsets(XTempData.defaultPageMarginLeft, 10, XTempData.defaultPageMarginRight, 0),
      isShowBottomDivider: false,
      leftWidget: XWidgetUtils.getLayoutContainer(
          child: XWidgetUtils.getWidgetText(item.name,style: XStyleUtils.getTextStyle(
              fontSize: XTempData.defaultTextFontSize,
              color: XColorUtils.color666666
          )),
          width: XTempData.defaultLeftKeyWidth
      ),
      rightWidget: XWidgetUtils.getLayoutRow(
          children: rightWidgets
      ),
    );

  }

  late String companyId;
  late String currentSocketServerOrClient;

  @override
  void onCreate() {
    super.onCreate();
    companyId=XSharePreferencesUtils.getString(RBConfigs.SP_companyId);
    currentSocketServerOrClient=XSharePreferencesUtils.getString('${RBConfigs.SP_currentServerOrClientSoscketPrinter}$companyId');
  }


  @override
  void onStart() {

    if(RBConfigs.versionType==VersionType.officialSelfServiceVersion
        || RBConfigs.versionType==VersionType.testSelfServiceVersion
    ){
      //自助收银
      setXTopBarWidget(
          height: null,
          child: RBWidgets_SelfService.getTitleBarWidget(
              this,
              title: '指定打印机设置',
          )
      );

    }else{
      //HD h5
      setXTitle(titleText: '指定打印机设置',titleBarHeight: 50,isShowLeftWidget: false);
      setXLeftFixSpace(width:458);
    }



    setXBottomBarWidget(
      child:XWidgetUtils.getXButtonRoundedRectAngle(
          text: '确认',
          fontColor: Colors.white,
          onPressed: () {
            if ('主设备'==currentSocketServerOrClient){
              //作为主设备
              XSharePreferencesUtils.saveString('${RBConfigs.SP_ServerSocket_Appoint_printer}$companyId',XJsonUtils.obj2Json(widget.listData));
            }else{
              //作为子设备
              XSharePreferencesUtils.saveString('${RBConfigs.SP_ClientSocket_Appoint_printer}$companyId',XJsonUtils.obj2Json(widget.listData));
            }

            finishPage(result: 'ok');
          },
          backgroundColor: RBColors.reabamColor4
      ),
      alignment: Alignment.center,
      padding: XWidgetUtils.getEdgeInsets(RBConfigs.bottomBarButtomMarginLeftRight, 0, RBConfigs.bottomBarButtomMarginLeftRight, 0),
      backgroundColor: Colors.white,
    );

  }


  @override
  Future<void> updateData({int pageIndex=1}) async {

    setState(() {

      //
      widget.listData.clear();
      List<BeanAppointPrinterScene> localAppointPrinterSettingList=[];


      String json='';
      if ('主设备'==currentSocketServerOrClient){
        //作为主设备
        json=XSharePreferencesUtils.getString('${RBConfigs.SP_ServerSocket_Appoint_printer}$companyId');
      }else{
        //作为子设备
        json=XSharePreferencesUtils.getString('${RBConfigs.SP_ClientSocket_Appoint_printer}$companyId');
      }

      //
      if (!XStringUtils.isEmpty(json)){
        List<dynamic> ll=XJsonUtils.json2List(json);
        for(dynamic item in ll){
          localAppointPrinterSettingList.add(BeanAppointPrinterScene.fromJson(item));
        }

      }else{

        //init
        List<BeanAppointPrinterIndex> deviceSerialNumbers1=[];
        List<BeanAppointPrinterIndex> deviceSerialNumbers2=[];
        List<BeanAppointPrinterIndex> deviceSerialNumbers3=[];
        List<BeanAppointPrinterIndex> deviceSerialNumbers4=[];
        List<BeanAppointPrinterIndex> deviceSerialNumbers5=[];
        List<BeanAppointPrinterIndex> deviceSerialNumbers6=[];
        for(int i=0;i<7;i++){
          deviceSerialNumbers1.add(BeanAppointPrinterIndex('${i+1}','${i+1}号',false));
          deviceSerialNumbers2.add(BeanAppointPrinterIndex('${i+1}','${i+1}号',false));
          deviceSerialNumbers3.add(BeanAppointPrinterIndex('${i+1}','${i+1}号',false));
          deviceSerialNumbers4.add(BeanAppointPrinterIndex('${i+1}','${i+1}号',false));
          deviceSerialNumbers5.add(BeanAppointPrinterIndex('${i+1}','${i+1}号',false));
          deviceSerialNumbers6.add(BeanAppointPrinterIndex('${i+1}','${i+1}号',false));
        }

        /*1外卖订单打印，2线下订单打印，3后厨打印，4标签打印  5.商城/点餐打印 ;*/
        BeanAppointPrinterScene scene1=BeanAppointPrinterScene('1','外卖订单打印',deviceSerialNumbers1);
        BeanAppointPrinterScene scene2=BeanAppointPrinterScene('2','线下订单打印',deviceSerialNumbers2);//销售单
        BeanAppointPrinterScene scene3=BeanAppointPrinterScene('3','后厨打印',deviceSerialNumbers3);
        BeanAppointPrinterScene scene4=BeanAppointPrinterScene('4','标签打印',deviceSerialNumbers4);
        BeanAppointPrinterScene scene5=BeanAppointPrinterScene('5','商城/点餐打印',deviceSerialNumbers5);
        BeanAppointPrinterScene scene6=BeanAppointPrinterScene('6','外卖后厨打印',deviceSerialNumbers6);

        localAppointPrinterSettingList.add(scene1);
        localAppointPrinterSettingList.add(scene2);
        localAppointPrinterSettingList.add(scene3);
        localAppointPrinterSettingList.add(scene4);
        localAppointPrinterSettingList.add(scene5);
        localAppointPrinterSettingList.add(scene6);


        if ('主设备'==currentSocketServerOrClient){
          //作为主设备
          XSharePreferencesUtils.saveString('${RBConfigs.SP_ServerSocket_Appoint_printer}$companyId',XJsonUtils.obj2Json(localAppointPrinterSettingList));
        }else{
          //作为子设备
          XSharePreferencesUtils.saveString('${RBConfigs.SP_ClientSocket_Appoint_printer}$companyId',XJsonUtils.obj2Json(localAppointPrinterSettingList));
        }

      }

      //
      widget.listData.addAll(localAppointPrinterSettingList);

    });
  }

}